<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>字符串</title>
    </head>
    <body>
        <script>
            // 1. 封装一个函数实现字符串翻转。
            let str1 = "hello world";
            function reserveStr(str) {
                return (str = [...str].reverse().join(""));
            }
            console.log(reserveStr(str1));
            // 2. 字符串 digite = "0123456789", 把后 5 个字符分割出来， 返回个单字符的数组, 注意， 返回数组里面的数字是字符串类型
            //期望结果:['5', '6', '7', '8', '9']
            let digite = "0123456789";
            function arrChar(str) {
                return (str1 = [...str.slice(str.length - 5)]);
            }
            console.log(arrChar(digite));
            // 3.已知有字符串 foo=”get-element-by-id”,写一个 function 将其转化成驼峰表示法”getElementById”.
            let foo = "get-element-by-id";
            function getHump(str) {
                let arr = [];
                for (let i = 0; i < str.length; i++) {
                    arr[i] = str[i - 1] === "-" ? str[i].toUpperCase() : str[i];
                }
                return arr.join("").split("-").join("");
            }
            console.log(getHump(foo));
            // 4.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面所有的"天"的下标输出
            let str3 = "今天是什么鬼天气，这么热，明天要去游泳！";
            function getDay(str) {
                let arr = [];
                for (let i = 0; i < str.length; i++) {
                    str[i] === "天" ? arr.push(i) : "";
                }
                return arr;
            }
            console.log(getDay(str3));
            // 5.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的所有"天"替换成"日"
            let str4 = "今天是什么鬼天气，这么热，明天要去游泳！";
            function func(str) {
                return (str = str.replaceAll("天", "日"));
            }
            console.log(func(str4));
            // 6.解析 URL Params 为对象，例如(https://www.baidu.com/s?ie=UTF-8&wd=letcode)得到{ie: 'UTF-8', wd: 'letcode', url: 'https://www.baidu.com/s'}
            // ie=UTF-8&wd=letcode
            // var arr = [ie = UTF-8, wd = letcode]
            // arr[0].indexOf("=")
            // var URLP =
            //     "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=gitee&fenlei=256&oq=%25E6%259F%25A5%25E6%2589%25BE%25E5%25AD%2597%25E7%25AC%25A6%25E4%25B8%25B2%25E4%25B8%25AD(abbcccddddd)%25E5%2587%25BA%25E7%258E%25B0%25E6%259C%2580%25E5%25A4%259A%25E7%259A%2584%25E5%25AD%2597%25E7%25AC%25A6%25E5%2592%258C%25E4%25B8%25AA%25E6%2595%25B0&rsv_pq=bb1cf3250000a32d&rsv_t=23c4P%2FAf06e2%2FkNcJRdaDbm9MmxxGfg%2FkoM8h7gTjk%2FUkvsSPuM9RX4Kh%2BM&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&inputT=4385&rsv_sug3=95&rsv_sug1=86&rsv_sug7=100&rsv_sug2=0&prefixsug=gitee&rsp=3&rsv_sug4=4385";
            var URLP = "https://www.baidu.com/s?ie=UTF-8&wd=letcode";
            let res = URLP.split("?")[1].split("&");
            let obj = {};
            for (let i = 0; i < res.length; i++) {
                let key = res[i].slice(0, res[i].indexOf("="));
                let value = res[i].slice(res[i].indexOf("=") + 1);
                obj[key] = value;
            }
            obj.url = URLP.split("?")[0];
            console.log(obj);
            // 7.查找字符串中(abbcccddddd)出现最多的字符和个数(Tips:要先统计每个字符出现的次数)
            let str7 = "abbcccddddd";
            let obj1 = {};
            let max = 0;
            let item = "";
            for (let i = 0; i < str7.length; i++) {
                if (obj1[str7[i]]) {
                    obj1[str7[i]] += 1;
                    if (max < obj1[str7[i]]) {
                        max = obj1[str7[i]];
                        item = str7[i];
                    }
                } else {
                    obj1[str7[i]] = 1;
                }
            }
            console.log(item, max);
        </script>
    </body>
</html>
